<?php

namespace Admin\Controller;


class CategoryController extends CommonController
{
    private $category;

    function __construct()
    {
        parent::__construct();
        $this->category = M('Category');
        $this->categories = $this->category->where("parent_id = 0")->select();
    }

    /***
     * 查出所有分组
     */
    private function all_categories()
    {
        $categories = $this->category->where("parent_id = 0")->order('sort_order')->select();
        foreach ($categories as $key => $value) {
            $children = $this->category->where("parent_id = $value[id]")->order('sort_order')->select();
            $categories[$key]['children'] = $children;
        }
        return $categories;
    }

    /***
     * 加载首页
     */
    public function index()
    {
        $count = $this->category->count();
        $Page = new \Common\Util\Page($count, 3);
        $this->categories = $this->category->order('sort_order')->
        limit($Page->firstRow . ',' . $Page->listRows)->select();

        $show = $Page->show();

        $this->assign('page',$show);
        $this->assign('count',$count);
        $this->categories = $this->all_categories();
        $this->display();
    }

    /***
     * 新增分组
     */
    public function add()
    {
        if (IS_POST) {
            $this->category->create();
            $this->category->add();
            $this->success('新增成功', U('Category/index'));

        } else {
            $this->display();
        }
    }

    /***
     * 编辑
     */
    public function edit()
    {
        $Category = M('Category');
        $id = I('get.id');


        if (IS_POST) {
            $this->category->create();
            $this->category->where("id = '$id'")->save();
            $this->success('编辑成功', U('Category/index'));

        } else {
            $cate = $Category->find($id);
            $this->assign('cate', $cate);
            $this->display();

        }

    }

    /***
     * ajax删除
     */
    public function delete()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $count = $this->category->where("parent_id = $id")->count();
            if ($count > 0) {
                $info = array('status' => 0, 'msg' => '此分类下有二级分类，不能删除');
            } else {
                $this->category->delete($id);
                $info = array('status' => 1, 'msg' => '删除成功');
            }
            $this->ajaxReturn($info);
        }

    }
    /***
     * 是否显示
     */
    public function change_attr(){
        if (IS_AJAX){
            $id = I('post.id');
            $is_show = !I('post.is_show');
            $this->category->where("id = '$id'")->setField("is_show",$is_show);
        }
    }

    /***
     * 排序
     */
    public function ajax_order(){
        if (IS_AJAX){
            $id = I('post.id');
            $sort_order = I('post.sort_order');
            $this->category->where("id = '$id'")->setField('sort_order', $sort_order);


        }
    }
    /***
     * 查看
     */
//    public function look(){
//        $id = I('get.id');
//        $Product = M('Product');
//        $Category = M('Category');
//
//        $products = $Product->where("status = 1")->where("category_id = '$id'")->
//        select();
//        $datas = array('is_top','is_new','is_hot','is_recommend','is_onsale');
//
//
//        foreach ($products as &$product) {
//            $product['category'] = $Category->where("id = '$product[category_id]'")->find();
//        }
//
//
//        $this->assign('products',$products);
//        $this->assign('datas', $datas);
//
//        $this->display();
//    }


}